package com.zyl.Ecs.auth.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.zyl.Ecs.auth.mapper.AccountRoleMapper;
import com.zyl.Ecs.auth.mapper.PermissionMapper;
import com.zyl.Ecs.auth.mapper.RoleMapper;
import com.zyl.Ecs.auth.mapper.RolePermissionMapper;
import com.zyl.Ecs.auth.mapper.ext.RoleExtMapper;
import com.zyl.Ecs.common.domain.auth.bean.AccountRole;
import com.zyl.Ecs.common.domain.auth.bean.Permission;
import com.zyl.Ecs.common.domain.auth.bean.Role;
import com.zyl.Ecs.common.domain.auth.bean.RolePermission;
import com.zyl.Ecs.common.domain.auth.ext.AccountExt;
import com.zyl.Ecs.common.domain.auth.ext.PermissionExt;
import com.zyl.Ecs.common.domain.auth.ext.RoleExt;
import com.zyl.Ecs.common.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.auth.service.impl
 * @ClassName: IService
 * @Description TODO
 * @Author 翟玉龙
 * @Date 2024/12/4-18:07
 */
@Service
@RequiredArgsConstructor
public class IService {

    private final AccountRoleMapper accountRoleMapper;
    private final RoleMapper roleMapper;
    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;
    private final RoleExtMapper roleExtMapper;

    /**
     * 填充权限
     * @param ext 用户扩展
     * @return {@link List }<{@link RoleExt }>
     */
    public List<RoleExt> fillRPs(AccountExt ext) {
        MPJLambdaWrapper<RoleExt> queryB = new MPJLambdaWrapper<>();
        queryB
                .selectAll(RoleExt.class)
                .selectCollection(RolePermission.class, RoleExt::getPermissionIds, map -> map
                        .result(RolePermission::getPermissionId)
                )
                .selectCollection(Permission.class, RoleExt::getPermissions)
                .leftJoin(RolePermission.class, RolePermission::getRoleId, RoleExt::getId)
                .leftJoin(Permission.class, Permission::getId, RolePermission::getPermissionId)
                .in(RoleExt::getId, ext.getRoleIds())
                .orderByAsc(RoleExt::getId)
                .orderByAsc(RolePermission::getPermissionId);
        return roleExtMapper.selectList(queryB);
    }

    /**
     * 填充权限
     * @return {@link List }<{@link RoleExt }>
     */
    public List<RoleExt> fillRPs() {
        MPJLambdaWrapper<RoleExt> queryB = new MPJLambdaWrapper<>();
        queryB
                .selectAll(RoleExt.class)
                .selectCollection(RolePermission.class, RoleExt::getPermissionIds, map -> map
                        .result(RolePermission::getPermissionId)
                )
                .selectCollection(Permission.class, RoleExt::getPermissions)
                .leftJoin(RolePermission.class, RolePermission::getRoleId, RoleExt::getId)
                .leftJoin(Permission.class, Permission::getId, RolePermission::getPermissionId)
                .orderByAsc(RoleExt::getId)
                .orderByAsc(RolePermission::getPermissionId);
        return roleExtMapper.selectList(queryB);
    }

    public AccountExt fillRoles(AccountExt accountExt, Integer accountId, boolean flag) {
        /* 查询用户角色关联表 */
        List<AccountRole> accountRoles = accountRoleMapper.selectList(Wrappers.<AccountRole>lambdaQuery()
                .eq(AccountRole::getAccountId, accountId));

        if (ObjectUtil.isEmpty(accountRoles)) {
            return accountExt;
        }

        /* 提取出所有的角色ID */
        List<Integer> roleIds = accountRoles.stream()
                .map(AccountRole::getRoleId)
                .collect(Collectors.toList());

        /* 根据角色ID列表查询角色信息 */
        List<Role> roles = roleMapper.selectBatchIds(roleIds);

        if (ObjectUtil.isEmpty(roles)) {
            return accountExt;
        }

        /* 为用户设置角色 */
        List<RoleExt> roleExts = roles.stream()
                .map(RoleExt::toExt)
                .collect(Collectors.toList());

        accountExt.setRoles(roleExts);

        if(flag) {
            return accountExt;
        }

        /* 遍历角色列表，查询权限信息 */
        for (RoleExt roleExt : roleExts) {
            Integer roleId = roleExt.getId();

            /* 查询角色权限关联 */
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(Wrappers.<RolePermission>lambdaQuery()
                    .eq(RolePermission::getRoleId, roleId));

            if (ObjectUtil.isEmpty(rolePermissions)) {
                continue;
            }

            /* 提取权限ID列表 */
            List<Integer> permissionIds = rolePermissions.stream()
                    .map(RolePermission::getPermissionId)
                    .collect(Collectors.toList());

            /* 查询权限信息 */
            List<Permission> permissions = permissionMapper.selectBatchIds(permissionIds);

            /* 权限列表转Ext类型 */
            List<PermissionExt> permissionExts = permissions.stream()
                    .map(PermissionExt::toExt)
                    .collect(Collectors.toList());

            roleExt.setPermissions(permissionExts);
        }

        return accountExt;
    }

}
